package concurrencyCookBook.forkAndJoin;

import java.util.List;
import java.util.concurrent.RecursiveAction;
//必须继承这个类
public class Task extends RecursiveAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4522711947894307886L;
	private List<Product> productList;
	private Integer first;
	private Integer last;
	private double increment;
	public Task(List<Product> productList,Integer first,Integer last,double increment){
		this.productList=productList;
		this.first=first;
		this.last=last;
		this.increment=increment;
	}

	@Override
	protected void compute() {
		if(last-first<10){
			//如果任务小于10个直接执行
			updatePrices();
		}else{
			//如果任务大于10个分任务
			Integer middle=(last+first)/2;
			Task t1=new Task(productList,first,middle+1,increment);
			Task t2=new Task(productList,middle+1,last,increment);
			//这里和executors差异
			invokeAll(t1,t2);
			
		}
	}
	private void updatePrices(){
		for(int i=first;i<last;i++){
			Product product=productList.get(i);
			product.setPrice((product.getPrice())*(1+increment));
		}
	}

}
